package rui.game.component;

import lombok.Getter;
import lombok.Setter;
import rui.game.move.MoveStrategy;

import javax.swing.*;
import java.awt.*;

/**
 * @author 赵睿
 */
@Getter
@Setter
public class Function {
    private MoveStrategy moveStrategy=new MoveStrategy();

    private JButton btnLeft;
    private JButton btnRight;
    private JButton btnUp;
    private JButton btnDown;

    public static Function init(Function function,GameFrame gameFrame){
        if(function==null){
            function=new Function();
            function.init(gameFrame.getContentPane());
        }
        return function;
    }

    public void init(Container con){
        initMoveBtn();
        con.add(btnLeft);
        con.add(btnRight);
        con.add(btnUp);
        con.add(btnDown);
    }

    protected void initMoveBtn(){
        btnLeft();
        btnRight();
        btnUp();
        btnDown();
        }

    private void btnLeft() {
        if(btnLeft!=null){
            return ;
        }
        btnLeft = new JButton("←");
        btnLeft.setFont(new Font("楷体", Font.BOLD, 15));
        btnLeft.setBounds(178, 483, 60, 40);
        btnLeft.addActionListener((event)->
                moveStrategy.getLeft().move());
    }
    private void btnRight() {
        if(btnRight!=null){
            return ;
        }
        btnRight = new JButton("→");
        btnRight.setFont(new Font("楷体", Font.BOLD, 15));
        btnRight.setBounds(330, 483, 60, 40);
        btnRight.addActionListener((event)->
                moveStrategy.getRight().move());
    }
    private void btnUp() {
        if(btnUp!=null){
            return ;
        }
        btnUp = new JButton("↑");
        btnUp.setFont(new Font("楷体", Font.BOLD, 15));
        btnUp.setBounds(249, 433, 60, 40);
        btnUp.addActionListener((event)->
                moveStrategy.getUp().move());
    }
    private void btnDown() {
        if(btnDown!=null){
            return ;
        }
        btnDown = new JButton("↓");
        btnDown.setFont(new Font("楷体", Font.BOLD, 15));
        btnDown.setBounds(249, 483, 60, 40);
        btnDown.addActionListener((event)->
                moveStrategy.getDown().move());

    }



}
